// packageB/pages/orderDetail/index.js
import {formatTime} from '../../../utils/util'
import Toast from '@vant/weapp/toast/toast';
import Dialog from '@vant/weapp/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:"",
    id:"",
    typelist:['待付款','待收货','已完成','退款中'],
    detailData:[],
    timeData:{},
    showAddress:false,
    changeInfo:{
        orderId:"",
        addressId:""
    },
    datas:[]
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      let index = parseFloat(options.status)-1
      // console.log(options)
      this.setData({
        type:this.data.typelist[index],
        id:options.id
      })
      wx.setNavigationBarTitle({
        title:this.data.type,
      })
      this.getDetail();
      this.getmyDress();
  },
  clearData(){
    this.setData({
      changeInfo:{
        orderId:"",
        addressId:"" 
      },
      showAddress:false
    })
  },
  onChange(e) {
    this.setData({
      timeData: e.detail
    });
  },
  //获取我的地址
  async getmyDress(){
    let res = await getApp().api.getAddress()
    // console.log(res)
    if(res.code!=200)return
    this.setData({
      datas:res.data
    })
  },
  //选择地址
 async chooseAddress(e){
       let items = e.currentTarget.dataset.item
       this.setData({
         ['changeInfo.addressId']:items.id,
         showAddress:false
       })
      // console.log(this.data.changeInfo)
       let res = await getApp().api.putAddress(this.data.changeInfo)
       // console.log(res)
       if(res.code!=200) return 
       Toast.success('地址已修改');
       this.setData({
        ['detailData[0].userName']:items.userName,
        ['detailData[0].phone']:items.telNum,
        ['detailData[0].address']:items.provinceName+items.cityName+items.detailInfo
       })
  },
  //倒计时结束
  overTime(){
    wx.navigateBack({
      delta: 2
    });
  },
  //获取订单详情
  async getDetail(){
     let res  = await getApp().api.orderDetail(this.data.id)
     // console.log(res)
     if(res.code!=200)return
     res.data[0].paymentTime?res.data[0].paymentTime = formatTime(new Date(res.data[0].paymentTime)):''
     res.data[0].deliveryTime!=null?res.data[0].deliveryTime = formatTime(new Date(res.data[0].deliveryTime)):'' 
     res.data[0].createTime = formatTime(new Date(res.data[0].createTime))
     this.setData({
      detailData:res.data
     })
   
  },
  //复制快递单号
  copys(){
    wx.setClipboardData({
      data: '218973346727422984',
      success (res) {
        wx.getClipboardData({
          success (res) { 
           // console.log(res.data) // data
          }
        })
      }
    })
  },
  //跳转物流详情
  getLogistic(e){
    // console.log(e)
    wx.navigateTo({
      url: '../logistics/index?com='+e.currentTarget.dataset.item.logistics+'&num='+e.currentTarget.dataset.item.logisticsNo,
    })
  },
  //改变地址
  changeMy(e){
    if(this.data.detailData[0].status!='1')return 
    this.setData({
      showAddress:true,
      ['changeInfo.orderId']:e.currentTarget.id,
    })
  },
  // 关闭弹出
  clearShow(){
    this.setData({
      showAddress:false
    })
  },
  //确认收获
   sureReceive(){
    Dialog.confirm({
      title: '',
      message: '确认收货？',
    })
    .then(async () => {
      // console.log(1)
      let res = await  getApp().api.receive(this.data.detailData[0].id)
      // console.log(res)
      if(res.code==200){
        wx.navigateBack({
          delta: 1
        })
      }
    })
    .catch(() => {
      // on cancel
    });
   
  },
  //支付
  async toPay(){
    // console.log(this.data.detailData)
   let data = {
    id: this.data.detailData[0].id,
    userMessage: "很好"
   }
   let payData = await  getApp().api.putPay(data)
   // console.log(payData)
   if(payData.code!=200) return wx.showToast({
    title: payData.msg,
    icon:"error"
  })  
   let returnPay = payData.data
    wx.requestPayment({
      ...returnPay,
      success :(res)=> { 
        // console.log('成功',res)
        if(res.errMsg=="requestPayment:ok"){
          Dialog.alert({
            message: '支付成功!',
            theme: 'round-button',
          }).then(() => {
            wx.navigateBack({
              delta: 1
            })
          }); 
        }
      },
      fail:(res)=>{
        // console.log('成功',res)
        if(res){
             wx.showToast({
               title: '支付失败！',
               icon:"error"
             })  
        }
       },
      complete:(res)=>{
        // console.log('综合',res)
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})